<?php
/*
Plugin Name: Xanga Cross Post
Plugin URI: http://tantannoodles.com/toolkit/xanga-crosspost/
Description: Cross post your blog entries to a Xanga account
Version: 0.2
Author: Joe Tan
Author URI: http://tantannoodles.com/
*/

class TanTanXangaPlugin {
    function admin() {
        require_once(dirname(__FILE__).'/xanga/lib.xanga.php');
        $xanga = new TanTanXanga();
        if ($_POST['action'] == 'save') {
            update_option('tantan_xanga_options', $_POST['options']);
        } elseif ($_POST['action'] == 'login') {
            if ($xanga->login($_POST['user'], $_POST['password'])) {
                update_option('tantan_xanga_session', $xanga->getSession());
                update_option('tantan_xanga_user', $_POST['user']);
                $message = "Successfully link your Xanga account.";
            } else {
                $error = "Error: ".$xanga->getError();
            }
        } elseif ($_POST['action'] == 'logout') {
            update_option('tantan_xanga_session', '');
        }
        
        $session = get_option('tantan_xanga_session');
        $user    = get_option('tantan_xanga_user');
        $options = get_option('tantan_xanga_options');
        
        $xanga->setOptions($options);
        include(dirname(__FILE__).'/xanga/admin-options.html');
    }
    function publish_post($post_id) {
        require_once(dirname(__FILE__).'/xanga/lib.xanga.php');
        $xanga = new TanTanXanga();
        $session = get_option('tantan_xanga_session');
        $user = get_option('tantan_xanga_user');
        if (!$session) {
            return $post_id;
        }
        $options = get_option('tantan_xanga_options');
        
        $xanga->setOptions($options);
        $xanga->setSession($session, $user);
        $xanga_post_id = $this->getXangaID($post_id);

        query_posts('p=' . $post_id);
		the_post();
		$content = get_the_content();
		$content = apply_filters('the_content', $content);
		$content = str_replace(']]>', ']]>', $content);

		$cats = get_the_category($ID);
		$cat_class = "tantan-entry ";
		if (is_array($cats)) foreach ($cats as $cat) {
		    $cat_class .= "$cat->category_nicename ";
		}
		$body = "<div class=\"$cat_class\">".
            "<h3><a href=\"".get_permalink()."\">".the_title('', '', false)."</a></h3>".
            $content;
		  
        $enclosure = get_post_meta($post_id, 'enclosure', true);
        if ($enclosure) {
            $mp3 = split("\n", $enclosure);
            if (!$mp3[2] || ($mp3[2] && ereg('audio',$mp3[2]))){
                $object = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="200" height="25" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" >'.
                    '<param name="movie" value="'.get_bloginfo('siteurl').'/wp-content/themes/tantan/mp3/mp3player.swf?config='.get_bloginfo('siteurl').'/wp-content/themes/tantan/mp3/config.xml&file='.
                        trim($mp3[0]).'" />' .
                    '<param name="wmode" value="transparent" />'.
                    '<embed src="'.get_bloginfo('siteurl').'/wp-content/themes/tantan/mp3/mp3player.swf?config='.get_bloginfo('siteurl').'/wp-content/themes/tantan/mp3/config.xml&file='.
                        trim($mp3[0]).'" wmode="transparent" width="200" height="25" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'.
                    '</object>';
                $body .= $object;
            }
		}

		$body .= "<p class=\"tantan-comments\"><em><a href=\"" . get_permalink() ."#comments\">Leave / read comments</a></em></p>\n";
		
		$body .= '</div>';

		$post = array(
            'title' => get_the_title(),
            'body' => $body,
		);
        
        if ($xanga_post_id) {
            $xanga->editPost($xanga_post_id, $post);
        } else {
            if ($xanga_post_id = $xanga->createPost($post)) {
                $this->setXangaID($post_id, $xanga_post_id);
            } else { // failed to create a xanga post
                echo 'hmm, error?';
            }
        }
    }
    function delete_post($post_id) {
        require_once(dirname(__FILE__).'/xanga/lib.xanga.php');
        $options = get_option('tantan_xanga_options');

        $xanga = new TanTanXanga();
        $xanga->setOptions($options);
        $session = get_option('tantan_xanga_session');
        $user = get_option('tantan_xanga_user');
        if (!$session) {
            return $post_id;
        }
        $xanga->setSession($session, $user);
        $xanga_post_id = $this->getXangaID($post_id);
        if ($xanga_post_id) {
            $xanga->deletePost($xanga_post_id);
        }
    }
    
    function activate() {}
    function deactivate() {}
    
    function addhooks() {
        add_options_page('Xanga', 'Xanga Crosspost', 10, __FILE__, array(&$this, 'admin'));
        add_action('publish_post', array(&$this, 'publish_post'), 9);
        add_action('delete_post', array(&$this, 'delete_post'), 9);
    }
    function setXangaID($post_id, $xanga_id) {
        if (!update_post_meta($post_id, 'xanga_id', $xanga_id)) {
            add_post_meta($post_id, 'xanga_id', $xanga_id);
        }
    }
    function getXangaID($post_id) {
        return get_post_meta($post_id, 'xanga_id', true);
    }

    function TanTanXangaPlugin() {
        add_action('admin_menu', array(&$this, 'addhooks'));
        //add_action('activate_tantan/xanga-crosspost.php', array(&$this, 'deactivate'));
        //add_action('deactivate_tantan/xanga-crosspost.php', array(&$this, 'deactivate'));
    }
}
$TanTanXangaPlugin =& new TanTanXangaPlugin();

?>